<template>
	<view class="mainBg h100 ">
		<z-paging ref="paging" v-model="list" @query="getList" :auto="false">
			<navbar :title="title">
				<navigator url="/pages/index/search">
					<image src="@/static/index/search.png" mode=""></image>
				</navigator>
			</navbar>
			<view class="cate">
				<text class="item" @click="selectCategory(item)" :class="{active:item.id == categoryId}"
					v-for="item,index in cate">{{item.name}}</text>
			</view>
			<!-- <view class="category-list">
				<view class="item" :class="category.id == categoryId ?`active`:``" v-for="(category,index) in categoryList"
					:key="category.id" @click="selectCategory(category)">{{category.name}}</view>
			</view> -->
			<view class="list container">
				<navigator :url="`./detail?id=${item.id}`" v-for="item,index in list" :key="item.id">
					<view class="col">
						<item :item="item" />
					</view>
				</navigator>
			</view>
		</z-paging>
	</view>
</template>

<script>
	import item from "./components/item.vue"
	export default {
		components: {
			item
		},
		data() {
			return {
				list: [],
				title: '',
				id: '',
				categoryId: '',
				parent_id: '',
				cate: []
			}
		},
		onLoad(option) {
			this.title = option.name
			this.categoryId = option.category_id
			this.parent_id = option.parent_id
			this.getCate()
		},
		methods: {
			getCate() {
				this.$post("/course/category_children", {
					parentId: this.parent_id
				}).then(res => {
					this.cate = res.data
					this.reload()
				})
			},
			selectCategory(item) {
				this.categoryId = item.id
				this.title = item.name
				this.reload()
			},
			reload() {
				this.$refs.paging.reload()
			},
			getList(pageNo, pageSize) {
				this.$get("/course/course_list", {
					pageNum: pageNo,
					pageSize: pageSize,
					categoryId: this.categoryId
				}).then(res => {
					//请勿在网络请求回调中给dataList赋值！！只需要调用complete就可以了
					this.$refs.paging.complete(res.data.rows);
				})
			},

		}
	}
</script>

<style lang="scss">
	page {
		background-color: #35CBAD;
	}

	.navBar uni-navigator {
		position: absolute;
		width: 40rpx;
		height: 40rpx;
		right: 40rpx;
		top: 50%;
		transform: translateY(-50%);
	}

	.cate {
		width: 100%;
		background-color: #fff;
		display: flex;
		overflow-x: auto;
		padding: 0 30rpx;

		.item {
			// width: 240rpx;
			padding: 20rpx 30rpx;
			text-align: center;
		}

		.active {
			color: #35CBAD;
		}
	}


	.list .col {
		padding: 40rpx 20rpx;
		margin: 20rpx 0;
		background-color: #fff;
		border-radius: 10rpx;
	}
</style>